package main

import (
	"AcServer/src/configration"
	"AcServer/src/controllers"
	"AcServer/src/models"
	"flag"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/server/web"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

var configFile = flag.String("c", "", "配置文件地址（绝对路径或者bin目录为基准的相对路径）")

func Inits() bool {
	flag.Parse()
	fmt.Printf("c:[ %s ]\n", *configFile)
	if *configFile == "" {
		flag.PrintDefaults()
		return false
	}
	return configration.InitData(*configFile)
}

func main() {
	if !Inits() {
		fmt.Printf("Init config file failure !")
		time.Sleep(1e9)
		return
	}

	orm.Debug = true
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", configration.Global.Mysql.AdminDsn)
	orm.RegisterModel(new(models.Job))
	orm.RegisterModel(new(models.Heart))
	orm.RegisterModel(new(models.Version))
	orm.RegisterModel(new(models.Device))

	web.BConfig.RunMode = "prod"
	web.BConfig.RouterCaseSensitive = true
	web.BConfig.EnableErrorsShow = false
	web.BConfig.EnableErrorsRender = false
	web.BConfig.WebConfig.AutoRender = false
	web.BConfig.CopyRequestBody = true

	web.ErrorController(&controllers.ErrorController{})
	web.Router("/job/addJob", &controllers.AddJobController{})
	web.Router("/job/getJob", &controllers.GetJobController{})
	web.Router("/job/updateJob", &controllers.UpdateJobController{})
	web.Router("/job/heartbeat", &controllers.HeartBeatController{})
	web.Router("/job/getHeartbeat", &controllers.GetHeartBeatController{})
	web.Router("/version/addVersion", &controllers.AddVersionController{})
	web.Router("/version/getVersion", &controllers.GetVersionController{})
	web.Router("/version/addVersionFile", &controllers.AddVersionFileController{})
	web.Router("/version/uploadFile", &controllers.UploadFileController{})
	web.Router("/version/deleteVersion", &controllers.DeleteVersionController{})
	web.Router("/device/add", &controllers.AddDeviceController{})
	web.Router("/device/getList", &controllers.DeviceGetListController{})

	//连接MQTT
	go controllers.ConnectAndSubscribe()

	web.Run(configration.Global.Server.Bindaddr)
}
